package TruongNhaLib;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

public class Schedule{
	private List<Lesson> Mon = new ArrayList<Lesson>();
	private List<Lesson> Wen = new ArrayList<Lesson>();
	private List<Lesson> Tue = new ArrayList<Lesson>();
	private List<Lesson> Thi = new ArrayList<Lesson>();
	private List<Lesson> Fri = new ArrayList<Lesson>();
	private List<Lesson> Sat = new ArrayList<Lesson>();
	private JSONArray jsonArr;
	private Lesson le;
	private int size;
	private String type ;
	public Schedule(JSONObject json, String userType){
		type = userType;
		Log.d("tupe", type);
		if(type.equals("3"))
			try {
				jsonArr = json.getJSONArray("2");
				size = jsonArr.length();
				if (size > 0)
				for (int i = 0; i < size; i++) {
					le = new Lesson(jsonArr.getJSONObject(i).getInt("time"), jsonArr.getJSONObject(i).getString("class"), jsonArr.getJSONObject(i).getString("subject"));
					Mon.add(le);
				}
				
				jsonArr = json.getJSONArray("3");
				size = jsonArr.length();
				if (size > 0)
				for (int i = 0; i < size; i++) {
					le = new Lesson(jsonArr.getJSONObject(i).getInt("time"), jsonArr.getJSONObject(i).getString("class"), jsonArr.getJSONObject(i).getString("subject"));
					Tue.add(le);
				}
				jsonArr = json.getJSONArray("4");
				size = jsonArr.length();
				if (size > 0)
				for (int i = 0; i < size; i++) {
					le = new Lesson(jsonArr.getJSONObject(i).getInt("time"), jsonArr.getJSONObject(i).getString("class"), jsonArr.getJSONObject(i).getString("subject"));
					Wen.add(le);
				}
				jsonArr = json.getJSONArray("5");
				size = jsonArr.length();
				if (size > 0)
				for (int i = 0; i < size; i++) {
					le = new Lesson(jsonArr.getJSONObject(i).getInt("time"), jsonArr.getJSONObject(i).getString("class"), jsonArr.getJSONObject(i).getString("subject"));
					Thi.add(le);
				}
				jsonArr = json.getJSONArray("6");
				size = jsonArr.length();
				if (size > 0)
				for (int i = 0; i < size; i++) {
					le = new Lesson(jsonArr.getJSONObject(i).getInt("time"), jsonArr.getJSONObject(i).getString("class"), jsonArr.getJSONObject(i).getString("subject"));
					Fri.add(le);
				}
				jsonArr = json.getJSONArray("7");
				size = jsonArr.length();
				if (size > 0)
				for (int i = 0; i < size; i++) {
					le = new Lesson(jsonArr.getJSONObject(i).getInt("time"), jsonArr.getJSONObject(i).getString("class"), jsonArr.getJSONObject(i).getString("subject"));
					Sat.add(le);
				}
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		if(type.equals("1"))
			try {
				jsonArr = json.getJSONArray("2");
				size = jsonArr.length();
				if (size > 0)
				for (int i = 0; i < size; i++) {
					le = new Lesson(jsonArr.getJSONObject(i).getInt("time"), jsonArr.getJSONObject(i).getString("subject"), jsonArr.getJSONObject(i).getString("teacher"));
					Mon.add(le);
				}
				
				jsonArr = json.getJSONArray("3");
				size = jsonArr.length();
				if (size > 0)
				for (int i = 0; i < size; i++) {
					le = new Lesson(jsonArr.getJSONObject(i).getInt("time"), jsonArr.getJSONObject(i).getString("subject"), jsonArr.getJSONObject(i).getString("teacher"));
					Tue.add(le);
				}
				jsonArr = json.getJSONArray("4");
				size = jsonArr.length();
				if (size > 0)
				for (int i = 0; i < size; i++) {
					le = new Lesson(jsonArr.getJSONObject(i).getInt("time"), jsonArr.getJSONObject(i).getString("subject"), jsonArr.getJSONObject(i).getString("teacher"));
					Wen.add(le);
				}
				jsonArr = json.getJSONArray("5");
				size = jsonArr.length();
				if (size > 0)
				for (int i = 0; i < size; i++) {
					le = new Lesson(jsonArr.getJSONObject(i).getInt("time"), jsonArr.getJSONObject(i).getString("subject"), jsonArr.getJSONObject(i).getString("teacher"));
					Thi.add(le);
				}
				jsonArr = json.getJSONArray("6");
				size = jsonArr.length();
				if (size > 0)
				for (int i = 0; i < size; i++) {
					le = new Lesson(jsonArr.getJSONObject(i).getInt("time"), jsonArr.getJSONObject(i).getString("subject"), jsonArr.getJSONObject(i).getString("teacher"));
					Fri.add(le);
				}
				jsonArr = json.getJSONArray("7");
				size = jsonArr.length();
				if (size > 0)
				for (int i = 0; i < size; i++) {
					le = new Lesson(jsonArr.getJSONObject(i).getInt("time"), jsonArr.getJSONObject(i).getString("subject"), jsonArr.getJSONObject(i).getString("teacher"));
					Sat.add(le);
				}
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}
	public void sortTimeOrder(List<Lesson> time){
		List<Lesson> _time =  new ArrayList<Lesson>();
		le = new Lesson(0, "", "");
		size = time.size(); /*
//		Log.d("size = ", size + " " );
		int min ;
		for( int i = 0 ; i < size ; i++ ){
//			Log.d("sort", "" + i);
			min = i ;
			for(int j = i + 1 ; j < size ; j++ ){
				if( time.get(j).getTime() < time.get(min).getTime() ){
					min = j ;
				}
			}//end
			if( min != i ){
				//swap
				le = time.get(min);
				time.set(min, time.get(i));
				time.set(i, le);
//				Log.d("swap i and min", i + " & " + min );
			}
		}//end
*/		//Add blank lessons to array list
		int t1,t2;
		t1 = 1; t2 = 1;
		for(int i = 0; i< size; i++){
			t2 = time.get(i).getTime();
			for(int j = t1; j< t2; j++){
				le = new Lesson( j , "", "");
				_time.add(le);
			}
			_time.add(time.get(i));
			if(t1 <= t2)
				t1 = t2+1;
		}
		t2 = 10;
		for(int j = t1; j<= t2; j++){
			le = new Lesson( j , "", "");
			_time.add(le);
		}
		time.clear();
		for (int i = 0; i < 10; i++) {
			time.add(_time.get(i));				
		}
 /*		for(int i = 0 ; i < 10 ; i++ ){
			if(i >= size ){
				le = new Lesson( i , "", "");
				time.add(i, le);				
			} else{
				if(time.get(i).getTime() != i){
					le = new Lesson( i , "", "");
					time.add(i, le);
				}
			}
		}//end for
*/	}//end sort
	public Schedule() {
		// TODO Auto-generated constructor stub
	}
	public List<Lesson> getMon(){
		sortTimeOrder(Mon);
		return Mon;
	}
	public List<Lesson> getWen(){
		sortTimeOrder(Wen);
		return Wen;
	}
	public List<Lesson> getTue(){
		sortTimeOrder(Tue);
		return Tue;
	}
	public List<Lesson> getThi(){
		sortTimeOrder(Thi);
		return Thi;
	}
	public List<Lesson> getFri(){
		sortTimeOrder(Fri);
		return Fri;
	}
	public List<Lesson> getSat(){
		sortTimeOrder(Sat);
		return Sat;
	}
}//end class